package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.stars.home.activity.ReceivedDetailsActivity
import com.stars.home.model.TechOrderDetail
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch
import java.util.ArrayList

class OrderDetailsVModel(application: Application) : BViewModel<BRepository>(application) {

    var bean=MutableLiveData<TechOrderDetail>()
    var id:String?=null
    //图片
    var nopath: ArrayList<String> = ArrayList() //全路径
    var type=MutableLiveData<String>()


    fun choseMoreImg(){
        //选择图片
        type.value="1"
    }
    fun clearMoreImg(position: Int) {
        nopath.removeAt(position)

    }


    fun setTips(technicianStatus:Int?,serviceTime:String?):String{

       when(technicianStatus){

           1->{
               return "订单进行中($serviceTime)"
           }
           2->{
               return "订单进行中($serviceTime)"
           }
           3->{
               return "订单进行中($serviceTime)"
           }
           4->{
               return "订单进行中($serviceTime)"
           }

       }

        return ""
    }


    fun setButContent(technicianStatus:Int?):String{

        when(technicianStatus){
            1->{
               return "立即出发"
            }
            2->{
                return "到达目的地"
            }
            3->{
                return "开始服务"
            }
            4->{
                return "服务结束"
            }
        }

        return ""
    }

    fun toChangeStatus(technicianStatus:Int?){

        when(technicianStatus){
            1->{
                setChangeStatus("2")
            }
            2->{
                setChangeStatus("3")
            }
            3->{
                setChangeStatus("4")
            }
            4->{
                setChangeStatus("5")
            }
        }

    }

    fun setChangeStatus(type:String){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.updateTechnicianStatus(id,type) }
            if(it.ok(true)){
                if(type=="5"){
                    UiSwitch.bundle(
                        repository().context,
                        ReceivedDetailsActivity::class.java, B().putString("id",id).ok()
                    )
                    ( repository().context as Activity).finish()
                }else {
                    //todo:刷新
                    getInfo()
                }
            }
        }
    }



    fun getInfo(){
        viewModelScope.launch{
            var it=repository().preq { NetEngine.service.technicianOrderDetail(id) }
            if(it.ok()){
                bean.value=it.data
            }

        }
    }


    fun toUpImg(){
        if(nopath.size==0||nopath.size<3){

            toast("请上传三张图片")
            return
        }
        var img=""
        for(item in nopath){
            img = if (img == ""){
                item
            }else{
                "$img,$item"
            }
        }
        viewModelScope.launch {
            val resp = repository().lreq { NetEngine.service.achieveImage(id,img) }
            if (resp.ok(true)) {
               //刷新
                getInfo()
            }
        }


    }


}
